// OSR b JVCS

package org.jvcs.client.core;

import org.jvcs.client.core.*;
import org.jvcs.common.*;
import org.jvcs.utils.*;

import java.io.*;
import java.net.*;
import java.util.*;

// Begin program
public class jvcs extends baseCS {

	
	// ============================================================================ //
	// main method
	// ============================================================================ //
	public static void main(String [] args) {
		// This is facing the user and accepting commands 
		// 1. load the props: global/common/local/this
		gini = osr.loadStdINI(G.GlobalINI);
		cini = osr.loadStdINI(G.CommonINI);
		lini = osr.loadStdINI(G.LocalINI);
		tini = osr.loadStdINI(G.ThisINI);
		
		PROG_NAME = gini.getProperty(G.PROGRAM_DISPLAY, G.NAME);
		
		// 2. parse the command line and understand
		CmdLine.parse(args);
		// 3. sort basic usage, version, help user queries
		helpBasicUsage();
		// 4. verbose on, print comments
		if (CmdLine.verboseFlag)
			helpDisplayDebug();
			
		// Command Dispatcher
		if (CmdLine.cmd.equalsIgnoreCase(G.S_checkout)) 	{ CheckoutHandler();}
		if (CmdLine.cmd.equalsIgnoreCase(G.S_checkin)) 		{ CheckinHandler();}
		if (CmdLine.cmd.equalsIgnoreCase(G.S_list)) 		{ ListHandler();}
		if (CmdLine.cmd.equalsIgnoreCase(G.S_status)) 		{ StatusHandler();}
		if (CmdLine.cmd.equalsIgnoreCase(G.S_init)) 		{ InitHandler();}
		if (CmdLine.cmd.equalsIgnoreCase(G.S_clone)) 		{ CloneHandler();}
		if (CmdLine.cmd.equalsIgnoreCase(G.S_create)) 		{ CreateHandler();}
		if (CmdLine.cmd.equalsIgnoreCase(G.S_update)) 		{ UpdateHandler();}
		if (CmdLine.cmd.equalsIgnoreCase(G.S_delete)) 		{ DeleteHandler();}
		if (CmdLine.cmd.equalsIgnoreCase(G.S_set))			{ SetHandler();}
		if (CmdLine.cmd.equalsIgnoreCase(G.S_unset)) 		{ UnsetHandler();}
		if (CmdLine.cmd.equalsIgnoreCase(G.S_get))			{ GetHandler();}
		if (CmdLine.cmd.equalsIgnoreCase(G.S_pull)) 		{ PullHandler();}
		if (CmdLine.cmd.equalsIgnoreCase(G.S_push)) 		{ PushHandler();}
		if (CmdLine.cmd.equalsIgnoreCase(G.S_import)) 		{ ImportHandler();}
		if (CmdLine.cmd.equalsIgnoreCase(G.S_export)) 		{ ExportHandler();}
		if (CmdLine.cmd.equalsIgnoreCase(G.S_incoming))	 	{ IncomingHandler();}
		if (CmdLine.cmd.equalsIgnoreCase(G.S_outgoing)) 	{ OutgoingHandler();}
		if (CmdLine.cmd.equalsIgnoreCase(G.S_patch)) 		{ PatchHandler();}
		if (CmdLine.cmd.equalsIgnoreCase(G.S_vendor)) 		{ VendorHandler();}
		if (CmdLine.cmd.equalsIgnoreCase(G.S_close)) 		{ CloseHandler();}
		if (CmdLine.cmd.equalsIgnoreCase(G.S_dump)) 		{ DumpHandler();}
		if (CmdLine.cmd.equalsIgnoreCase(G.S_grep)) 		{ GrepHandler();}
		
		if (CmdLine.cmd.equalsIgnoreCase(G.S_finddir)) 		{ FindDirHandler();}
		if (CmdLine.cmd.equalsIgnoreCase(G.S_findfile))		{ FindFileHandler();}
		if (CmdLine.cmd.equalsIgnoreCase(G.S_findall))		{ FindAllHandler();}
		
	} // END MAIN METHOD
	
	// FIND dir, file, all
	public static void FindDirHandler() {
		if (CmdLine.verboseFlag) osr.pl("FindDirHandler");
		int argsLength = CmdLine.argAL.size();
		if (CmdLine.verboseFlag) osr.pl("FindDirHandler Arg: "+argsLength);
		
		if (argsLength == 0) { osr.pl(osr.gm(gini, G.U_usage, G.U_find_dir)); osr.e(); }
		
		String dir = ".";
		String wc = "*";
		if (argsLength == 1) {			
			wc = CmdLine.argAL.get(0);
			Find.findDir(dir, wc);
		}
		if (argsLength == 2) {			
			wc = CmdLine.argAL.get(0);
			dir = CmdLine.argAL.get(1);
			Find.findDir(dir, wc);
		}
		if (argsLength == 0) {			
			Find.sWildCard = wc;
			Find.findDir(dir, wc);
		}
	}
	public static void FindFileHandler() {
		if (CmdLine.verboseFlag) osr.pl("FindFileHandler");
		int argsLength = CmdLine.argAL.size();
		if (CmdLine.verboseFlag) osr.pl("FindFileHandler Arg: "+argsLength);
		
		if (argsLength == 0) { osr.pl(osr.gm(gini, G.U_usage, G.U_find_file)); osr.e(); }
		
		String dir = ".";
		String wc = "*";
		if (argsLength == 1) {			
			wc = CmdLine.argAL.get(0);
			Find.findFile(dir, wc);
		}
		if (argsLength == 2) {			
			wc = CmdLine.argAL.get(0);
			dir = CmdLine.argAL.get(1);
			Find.findFile(dir, wc);
		}
		if (argsLength == 0) {			
			Find.findFile(dir, wc);
		}
	}
	public static void FindAllHandler() {
		if (CmdLine.verboseFlag) osr.pl("FindAllHandler");
		int argsLength = CmdLine.argAL.size();
		if (CmdLine.verboseFlag) osr.pl("FindAllHandler Arg: "+argsLength);
		
		if (argsLength == 0) { osr.pl(osr.gm(gini, G.U_usage, G.U_find_all)); osr.e(); }
		
		String dir = ".";
		String wc = "*";
		if (argsLength == 1) {			
			wc = CmdLine.argAL.get(0);
			Find.findAll(dir, wc);
		}
		if (argsLength == 2) {			
			wc = CmdLine.argAL.get(0);
			dir = CmdLine.argAL.get(1);
			Find.findAll(dir, wc);
		}
		if (argsLength == 0) {			
			Find.findAll(dir, wc);
		}
	}
	// END FILE 
	public static void CloseHandler() {
		if (CmdLine.verboseFlag) osr.pl("CloseHandler");
	}
	public static void VendorHandler() {
		if (CmdLine.verboseFlag) osr.pl("VendorHandler");
	}
	public static void PatchHandler() {
		if (CmdLine.verboseFlag) osr.pl("PatchHandler");
	}
	public static void OutgoingHandler() {
		if (CmdLine.verboseFlag) osr.pl("OutgoingHandler");
	}
	public static void IncomingHandler() {
		if (CmdLine.verboseFlag) osr.pl("IncomingHandler");
	}
	public static void ExportHandler() {
		if (CmdLine.verboseFlag) osr.pl("ExportHandler");
	}
	public static void ImportHandler() {
		if (CmdLine.verboseFlag) osr.pl("ImportHandler");
	}
	public static void PushHandler() {
		if (CmdLine.verboseFlag) osr.pl("PushHandler");
	}
	public static void PullHandler() {
		if (CmdLine.verboseFlag) osr.pl("PullHandler");
	}
	public static void CheckoutHandler() {
		if (CmdLine.verboseFlag) osr.pl("CheckoutHandler");
	}
	public static void CheckinHandler() {
		if (CmdLine.verboseFlag) osr.pl("CheckinHandler");
	}
	public static void ListHandler() {
		if (CmdLine.verboseFlag) osr.pl("ListHandler");
	}
	public static void StatusHandler() {
		if (CmdLine.verboseFlag) osr.pl("StatusHandler");
	}
	public static void InitHandler() {
		if (CmdLine.verboseFlag) osr.pl("InitHandler");
	}
	public static void CloneHandler() {
		if (CmdLine.verboseFlag) osr.pl("CloneHandler");
	}
	public static void GrepHandler() {
		int argsLength = CmdLine.argAL.size();
		if (CmdLine.verboseFlag) osr.pl("GrepHandler");
			
		if (argsLength == 0) { osr.pl(osr.gm(gini, G.U_usage, G.U_grep)); osr.e(); }
		
		try {
			boolean caseSensitive = true;
			if (CmdLine.iFlag) caseSensitive = false;
			if ( (argsLength == 1) || (argsLength == 2)) {
				// <search-string> <dir>
				if (argsLength == 2) {
					ListGrepFiles.sCase = caseSensitive;
					ListGrepFiles.sDir = CmdLine.argAL.get(1);
					ListGrepFiles.sStr = CmdLine.argAL.get(0);
					ListGrepFiles.launch();
				}
				// <search-string>
				if (argsLength == 1) {
					ListGrepFiles.sCase = caseSensitive;
					ListGrepFiles.sDir = ".";
					ListGrepFiles.sStr = CmdLine.argAL.get(0);
					ListGrepFiles.launch();
				}						
			}
		} catch (Exception e1) {
			osr.ee("LGF Error: "+e1.toString());
		}
		
		
	}
	public static void CreateHandler() {		
		int argsLength = CmdLine.argAL.size();
		if (CmdLine.verboseFlag) osr.pl("CreateHandler Args: "+argsLength);
		//
		String cdir = "";
		String action = "";
		if (argsLength > 1) {		
			action = CmdLine.argAL.get(0);
			if (CmdLine.verboseFlag) osr.pl("Action: "+action);
			// hub, project, task, issue, release, build, branch, tag
			if (action.equalsIgnoreCase(G.S_hub)) {
				if (CmdLine.verboseFlag) osr.pl("Create Hub");
				createHubHandler();
			} else if (action.equalsIgnoreCase(G.S_project)) {
				if (CmdLine.verboseFlag) osr.pl("Create Project");
				createProjectHandler();
			} else if (action.equalsIgnoreCase(G.S_task)) {
				if (CmdLine.verboseFlag) osr.pl("Create Task");
				createTaskHandler();
			} else if (action.equalsIgnoreCase(G.S_issue)) {
				if (CmdLine.verboseFlag) osr.pl("Create Issue");
				createIssueHandler();
			} else if (action.equalsIgnoreCase(G.S_release)) {
				if (CmdLine.verboseFlag) osr.pl("Create Release");
				createReleaseHandler();			
			} else if (action.equalsIgnoreCase(G.S_build)) {
				if (CmdLine.verboseFlag) osr.pl("Create Build");
				createBuildHandler();
			} else if (action.equalsIgnoreCase(G.S_branch)) {
				if (CmdLine.verboseFlag) osr.pl("Create Branch");
				createBranchHandler();
			} else if (action.equalsIgnoreCase(G.S_tag)) {
				if (CmdLine.verboseFlag) osr.pl("Create Tag");
				createTagHandler();
			} else if (action.equalsIgnoreCase(G.S_user)) {
				if (CmdLine.verboseFlag) osr.pl("Create Tag");
				createUserHandler();
			} else if (action.equalsIgnoreCase(G.S_group)) {
				if (CmdLine.verboseFlag) osr.pl("Create Tag");
				createGroupHandler();
			} else {
				osr.ee("Unknown command: "+action);
			}
		} else {
			osr.pl(osr.gm(gini, G.U_usage, G.U_create)); osr.e();
		}
		
	}
	public static void DeleteHandler() {
		if (CmdLine.verboseFlag) osr.pl("DeleteHandler");
	}
	public static void UpdateHandler() {
		if (CmdLine.verboseFlag) osr.pl("UpdateHandler");
	}
	
	
}
// End program